package yong.birthdayreminder;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ReminderShowBox extends Activity implements OnClickListener {
	
	private Button b1 = null;
	private Button b2 = null;
	private TextView tv = null;
	private String showinfo = null;
	private String birthday = null;
	private long start;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.remindinfo);
		showinfo = this.getIntent().getStringExtra("reminderstring");
		birthday = this.getIntent().getStringExtra("birthday");
		start = this.getIntent().getLongExtra("start", 0);
		b1 = (Button) findViewById(R.id.Button01);
		b1.setText(R.string.iknow);
		b2 = (Button) findViewById(R.id.Button02);
		b2.setText(R.string.remindermelater);
		tv = (TextView) findViewById(R.id.TextView01);
		tv.setText(showinfo + birthday);
		b1.setOnClickListener(this);
		b2.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.Button01) {
			this.finish();
		} else if (id == R.id.Button02) {
			this.finish();
			long remindDate = System.currentTimeMillis()
					+ AlarmManager.INTERVAL_DAY;
			//Delay one day's reminder if this remindDate is still before the birthday
			if (remindDate < start) {
				AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
				Intent intentShowBirthday = new Intent(this,
						ReminderShowBox.class);
				// Put the Show information in the intent
				intentShowBirthday.putExtra("reminderstring", showinfo);
				intentShowBirthday.putExtra("birthday", birthday);
				// use the System.currentTimeMillis as the second parameter:
				// requestCode. This code needs to be sole if you want to
				// implement multiple alarm.
				PendingIntent pendingIntent = PendingIntent
						.getActivity(this, (int) System.currentTimeMillis(),
								intentShowBirthday, 0);
				alarmManager.set(AlarmManager.RTC, System.currentTimeMillis()
						+ AlarmManager.INTERVAL_DAY, pendingIntent);
			} else {
				this.finish();
			}

		}
		// TODO Auto-generated method stub

	}

}
